import axios from 'axios';
import {ElMessage} from 'element-plus'

const toast = ElMessage
const headapi = "/api/"

function getApiBasic(url: string, postdata?: any) {
    return axios.get(url, {params: postdata}).then(function (data) {
        let json = data.data;
        if (json["code"] === 500) {
            toast.error("系统错误，请联系管理员");
            return json
        }
        if (json["code"] === 401) {
            localStorage.removeItem('token');
            toast.error(json["msg"]);
            return false
        } else if (json["code"] === 403) {
            toast.error(json["msg"]);
            return json
        } else {
            return json
        }
    }, function (response) {
        if (response) {
            toast.error(`${response.config.url} is ${response.status} ${response.statusText}`)
        } else {
            toast.error(`${response.config.url} is ${response.message}`)
        }
    })
}

function getApiPost(url: string, postdata?: any) {
    return axios.post(url, postdata).then(function (data) {
        let json = data.data;
        if (json["code"] === 500) {
            toast.error("系统错误，请联系管理员");
            return json
        }
        if (json["code"] === 401) {
            localStorage.removeItem('token');
            toast.error(json["msg"]);
            return false
        } else if (json["code"] === 403) {
            toast.error(json["msg"]);
            return json
        } else {
            return json
        }
    }, function (response) {
        if (response) {
            toast.error(`${response.config.url} is ${response.status} ${response.statusText}`)
        } else {
            toast.error(`${response.config.url} is ${response.message}`)
        }
    })
}

// 正式接口
export function getoverdueList(postdate?: any) {
    let url = headapi + 'data/syntheticalmap/overdueList';
    return getApiBasic(url, postdate);
}

export function captchaImage(postdate?: any) {
    let url = headapi + 'data/captchaImage';
    return getApiPost(url, postdate);
}

export function noVerLogin(postdate?: any) {
    let url = headapi + 'data/noVerLogin';
    return getApiPost(url, postdate);
}


